#!/bin/bash
# SCRIPT  : execsum.sh
# PURPOSE : A menu driven Shell Script 
#           To select the simulation package and the corresponding files for extracting 
#           executive summary for transfering data to Tardis.
#
##############################################################################
#Global variable declaration. 
##############################################################################
#varibale to store the package name vasp,gulp,seista,crystal 
PACKAGE=""
#varibale to store the facility name NCI-NF,RMIT-HPC, VPAC
FACILITY=""
#varibale to store the directory from which the data is curated
searchdir=""
#varibale to store HOME directory
HOME=~
#varibale to hold the base directory
BASEDIR="$HOME/Scripts"
#varibale to store the staging area where the directories resides
EXPORTDIR="$BASEDIR/exportdir"
#varibale to store the tmp files
TMPDIR="$BASEDIR/tmp"
#varibale to store the counter file
COUNTDIR="$BASEDIR/count"
#variable to store log files
LOGDIR="$BASEDIR/log"
#variable to store the description
DESC="Press ok to Continue"
#variable to store the Experiment Name
EXP=""
#varibale to store the Project Name
PRO=""
#variable to store the Experiment Name for the folder
FEXP=""
#
#variable to store the *.o* file in the directory
OUTFILE=""
#List of selected files LOF
LOF="" 
#
#Canned Text Variables
##System Date
DATE=`date`
NCPU="[NCPU]"
VRAM="[VRAM]"
JOBFS="[JOBFS]"
WALLTIME="[WALLTIME]"
STOICHIOMETRY="[STOICHIOMETRY]"
NATOMS="[NATOMS]"
XCFUNCTIONAL="[XCFUNCTIONAL]"
CUTOFFTYPE="[CUTOFFTYPE]"
CUTOFF="[CUTOFFTYPE]"

####Canned Text
CANNEDTEXT="This was a $PACKAGE calculation performed on $FACILITY, using $NCPU cpus, \
$VRAM GB of RAM, and $JOBFS GB of disk space for $WALLTIME hours, on $DATE.  The job consisted of $EXP, \
with $STOICHIOMETRY stoichiometry, and $NATOMS atoms, calculated using the $XCFUNCTIONAL functional with $CUTOFFTYPE \
set to $CUTOFF eV.  The run was performed by $FULLNAME under project $PRO."

### Tardis Username Password ####
TUSERNAME=""
TPASSWD=""
URL="http://gaia1.isis.rmit.edu.au:8890"


###############################################################################
#                 Checking availability of dialog utility                    #
#
# dialog is a utility installed by default on all major Linux distributions. #
# But it is good to check availability of dialog utility on your Linux box.  #
##############################################################################
which dialog &> /dev/null

[ $? -ne 0 ]  && echo "Dialog utility is not available, Install it" && exit 1

##############################################################################
#                      Define Functions Here                                 #
##############################################################################

###################### deletetempfiles function ##############################

# This function is called by trap command
# For conformation of deletion use rm -fi *.$$

deletetempfiles()
{
    rm -f $TMPDIR/file.$PACKAGE.*
    rm -f $TMPDIR/menuchoices.$$
    rm -f $TMPDIR/metadata.$PACKAGE.*
    rm -f $TMPDIR/metadata.* 
}


######################## Show_time function #################################


####################### Get Package Name and Search  file function #################################

# Get the package name from the menu, search for files after checking the directory 
# Create tmp files with the selected files.

pack_files()
{

  PACKAGE=$1

  # echo $PACKAGE
  # rm -f file.$PACKAGE.*
  # TODO
  # Find whether the value of PBS is -wd is vasp_sub for pwd 

   dialog --backtitle "DATASET SUBMISSION SCRIPT" --title "Search Directory Path" \
--inputbox "\nEnter directory path (Absolute or Relative) \
\nPress just Enter for current directory" 12 60 "`pwd`" 2> $TMPDIR/file.$PACKAGE.path


   if [ $? -ne 0 ]
   then
       rm -f $TMPDIR/file.$PACKAGE.path
       return
   fi

   searchdir=`cat $TMPDIR/file.$PACKAGE.path`


   if ! [[ -z "$searchdir" ]]
   then
       # remove trailing * and / from directory path
       echo "$searchdir" | grep "\*$" &> /dev/null && rmdir=${searchdir%\*}
       echo "$searchdir" | grep "/$" &> /dev/null && rmdir=${searchdir%/}

       ( cd $searchdir 2>&1 | grep "No such file or directory" &> /dev/null )
       if [ $? -eq 0 ]
       then
           dialog --backtitle "MENU DRIVEN PROGRAM" \
           --title "Validating Directory" \
           --msgbox "\n $searchdir: No such file or directory \
\n\n Press ENTER to return to the Main Menu" 10 60
           return
       fi

       #TODO
       #Check for $FILELIST variable and use the select all option

       touch $TMPDIR/file.$PACKAGE.list
       count=0

       if [ "$PACKAGE" == "vasp" ]
       then
          for item in `find $searchdir -maxdepth 1 -type f \( -name "CONTCAR*" -o -name "INCAR*" -o -name "KPOINTS*" -o -name "OSZICAR*" -o \
-name "OUTCAR*" -o -name "POSCAR*" -o -name "vasp*" -o -name "*.o*" -o -name "*.e*" -o -name "POTCAR*" -o -name "*sub" -o -name "vasprun.xml" \)`
          do
 	      count=`expr $count + 1`
  	      echo $count `basename $item` on >> $TMPDIR/file.$PACKAGE.list
          done
       elif [ "$PACKAGE" == "siesta" ]
       then
          for item in `find $searchdir -maxdepth 1 -type f \( -name "input.fdf*" -o -name "*.STRUCT_IN*" -o -name "seista*" -o -name "*.o*" -o \
-name "*.e*" -o -name "*.psf*" -o -name "*.xml*" -o -name "*sub*" -o -name "output*" -o -name "*.STRUCT_OUT*" \)`
          do
              count=`expr $count + 1`
              echo $count `basename $item` on >> $TMPDIR/file.$PACKAGE.list
          done
       elif [ "$PACKAGE" == "crystal" ]
       then
          for item in `find $searchdir -maxdepth 1 -type f \( -name "INPUT*" -o -name "OUTPUT*" -o -name "*.sub" -o -name "*.o*" -o \
-name "*.e*" -o -name "*.in*" -o -name "*.DAT*" -o -name "*.[0-9]" -o -name "*.[0-9][0-9]" \)`
          do
              count=`expr $count + 1`
              echo $count `basename $item` on >> $TMPDIR/file.$PACKAGE.list
          done
       elif [ "$PACKAGE" == "gulp" ]
       then
          for item in `find $searchdir -maxdepth 1 -type f \( -name "*.gin" -o -name "*.gout" -o -name "*.sub" -o -name "*.o*" -o \
-name "*.e*" -o -name "*" \)`
          do
              count=`expr $count + 1`
              echo $count `basename $item` on >> $TMPDIR/file.$PACKAGE.list
          done
       else
          dialog --backtitle "MENU DRIVEN PROGRAM" \
          --title "Validating Directory" \
          --msgbox "\n Unknown Choice \
\n\n Press ENTER to return to the Main Menu" 10 60
          return
       fi

# filelist stores the height depending on the length of the files
       if [ $count -ne 0 ]
       then
          let filelist=$count*10
          FILELIST=`cat $TMPDIR/file.$PACKAGE.list`

          if [ $filelist -le 21 ]
          then
             filelist=50
          fi

          if [ $filelist -gt 100 ]
          then
             filelist=70
             count=25
          fi

#         echo "Filelist $filelist $FILELIST"

          dialog --backtitle "DATASET SUBMISSION SCRIPT" --title "List of Package Files" \
--checklist "Files to be transfered. Deselect files using SPACE BAR : " 30 $filelist $count $FILELIST 2> $TMPDIR/file.$PACKAGE.selected

          if [ $? -eq 0 ]
          then
             touch $TMPDIR/file.$PACKAGE.send 
             FILELIST=`cat $TMPDIR/file.$PACKAGE.selected`
             for choice in $FILELIST
             do
                 grep -w "`echo $choice | sed 's/"//g'`" $TMPDIR/file.$PACKAGE.list | awk '{ print $2 }' >> $TMPDIR/file.$PACKAGE.send
	     done
          else
             return
          fi
          file_desc
       else
           dialog --backtitle "MENU DRIVEN PROGRAM" \
           --title "Search Result" \
           --msgbox "\n $searchdir: No related files for the $PACKAGE package \
\n\n Press ENTER to return to the Main Menu" 10 60
           return
       fi
   else
       rm -f $TMPDIR/file.$PACKAGE.*
       return
   fi

}

file_desc(){

  HEIGHT=$(cat $TMPDIR/file.$PACKAGE.send | wc -l)
  let HEIGHT=$HEIGHT*10


  if [ $HEIGHT -le 21 ]
  then
     HEIGHT=50
  fi

  DESC="Press ok to Continue"

  if [ $HEIGHT -gt 70 ]
  then
     dialog --backtitle "DATASET SUBMISSION SCRIPT" --title "Enter Description for Files" --inputbox "Selected Files List is too BIG to display" 20 50 \
"$DESC" 2> $TMPDIR/metadata.$PACKAGE
  else
     dialog --backtitle "DATASET SUBMISSION SCRIPT" --title "Enter Description for Files" --inputbox "`cat $TMPDIR/file.$PACKAGE.send`" 30 $HEIGHT \
"$DESC" 2> $TMPDIR/metadata.$PACKAGE
  fi


  if [ $? -eq 0 ]
  then
     DESC=`cat $TMPDIR/metadata.$PACKAGE`
     if [[ -z $DESC ]]
     then
        dialog --msgbox "\nNeed brief description about the experiment to submit the dataset.\nPress any key to proceed..." 15 15
        file_desc
     else
        file_exp
     fi
  else
     return
  fi

}

error_function(){

  status=$?
  echo "The error status was $status"

}

file_exp(){

  if [ "$PACKAGE" == "vasp" ]
  then
     if [[ -e $searchdir/INCAR ]]
     then
        ( cat $searchdir/INCAR )
        EXPNAME=`cat $searchdir/INCAR | grep "System"| awk -F= '{ print $2 }' | awk '{for(i=1;i<=NF;i++)print $i}' | tr -s '\n' ' '`
     else
        EXPNAME="No INCAR file found. Enter Experiment Name"
     fi
  elif [ "$PACKAGE" == "siesta" ]
  then
     if [[ -e $searchdir/input.fdf ]]
     then
        EXPNAME=`cat $searchdir/input.fdf | grep "SystemName" | awk '{for(i=2;i<=NF;i++)print $i}' | tr -s '\n' ' '` 
     else
        EXPNAME="No input.fdf file found. Enter Experiment Name"
     fi
  elif [ "$PACKAGE" == "crystal" ]
  then
     if [[ -e $searchdir/INPUT ]]
     then
        EXPNAME=`cat $searchdir/INPUT | head -1`
     else
        EXPNAME="No INPUT file found. Eneter Experiment Name."
     fi
  elif [ "$PACKAGE" == "gulp" ]
  then
     if [[ -e $searchdir/input.fdf ]]
     then
        EXPNAME=`cat $searchdir/input.fdf | grep "SystemName" | awk '{for(i=2;i<=NF;i++)print $i}' | tr -s '\n' ' '` 
     else
        EXPNAME="Please Enter Experiment Name"
     fi
  else
     return
  fi

  LOF=""
  HEIGHT=$(cat $TMPDIR/file.$PACKAGE.send | wc -l)
  let HEIGHT=$HEIGHT*10
  LOF=`cat $TMPDIR/file.$PACKAGE.send`
  if [ $HEIGHT -le 21 ]
  then
     HEIGHT=50
  fi

  if [ $HEIGHT -gt 70 ]
  then
     dialog --backtitle "DATASET SUBMISSION SCRIPT" --title "List of Selected Experiment " --clear --separate-widget "              " --msgbox \
"$LOF" 20 50 --title "Do you want to update the Experiment Name " --inputbox "Experiment Name" 20 50 "$EXPNAME" 2> $TMPDIR/metadata.$PACKAGE
  else
     dialog --backtitle "DATASET SUBMISSION SCRIPT" --title "Do you want to update the Experiment Name " --inputbox "Files:`cat $TMPDIR/file.$PACKAGE.send`" 30 50 \
"$EXPNAME" 2> $TMPDIR/metadata.$PACKAGE
  fi

  if [ $? -eq 0 ]
  then
     EXP=`cat $TMPDIR/metadata.$PACKAGE`
     if [[ -z $EXP ]]
     then
        dialog --msgbox "\nExperiment name should be entered.\nPress any key to proceed..." 15 15
        file_exp
     else
        file_project
     fi
  else
     dialog --msgbox "\nError in displaying the experiment dialog box. The error code is $?.Press any key to go back to Main menu" \
15 40
     return
  fi
}

file_project(){

#  TODO grep the project name from the correct files


  (ls $searchdir/*.o*)
  if [ $? -eq 0 ]
  then
     OUTFILE=`ls $searchdir/*.o* | sort -n -r | head -1`
  fi


  STOICHIOMETRY="[STOICHIOMETRY]"
  NATOMS="[NATOMS]"
  XCFUNCTIONAL="[XCFUNCTIONAL]"
  CUTOFFTYPE="[CUTOFFTYPE]"
  CUTOFF="[CUTOFFTYPE]"


  if [[ -z $OUTFILE ]]
  then
     PRONAME="No *.o* file found. Enter Project Name"
  else

#     echo "The actual output of $OUTFILE"
#     echo `basename $OUTFILE`

     if [ "$PACKAGE" == "vasp" ]
     then
        PRONAME=`cat $searchdir/basename $OUTFILE | grep "Project:" | awk -F: '{ print $2}' | awk '{for(i=1;i<=(NF-2);i++)print $i}'`
        VRAM=`cat $searchdir/basename $OUTFILE | grep "Max virtual memory" | awk -F' ' '{ print $4}'`
        NCPU=`cat $searchdir/basename $OUTFILE | grep "Number of cpus" | awk -F' ' '{ print $4}'`
        JOBFS=`cat $searchdir/basename $OUTFILE | grep "Max jobfs disk use" | awk -F' ' '{ print $5}'`
        WALLTIME=`cat $searchdir/basename $OUTFILE | grep "Elapsed time" | awk -F' ' '{ print $5}'`
     elif [ "$PACKAGE" == "siesta" ]
     then
	PRONAME=`cat $searchdir/basename $OUTFILE | grep "Project:" | awk -F: '{ print $2}' | awk '{for(i=1;i<=(NF-2);i++)print $i}'`
        VRAM=`cat $searchdir/basename $OUTFILE | grep "Max virtual memory" | awk -F' ' '{ print $4}'`
        NCPU=`cat $searchdir/basename $OUTFILE | grep "Number of cpus" | awk -F' ' '{ print $4}'`
        JOBFS=`cat $searchdir/basename $OUTFILE | grep "Max jobfs disk use" | awk -F' ' '{ print $5}'`
        WALLTIME=`cat $searchdir/basename $OUTFILE | grep "Elapsed time" | awk -F' ' '{ print $5}'`

     elif [ "$PACKAGE" == "crystal" ]
     then
#       PRONAME=`cat $searchdir/vasp*sub | grep "#PBS -P" | awk '{ print $3 }'`
#       PRONAME=`cat $searchdir/vasp*.o* | grep "Project:" | awk '{for(i=2;i<=NF;i++)print $i}'`
        PRONAME=`cat $searchdir/basename $OUTFILE | grep "Project:" | awk -F: '{ print $2}' | awk '{for(i=1;i<=(NF-2);i++)print $i}'`
        VRAM=`cat $searchdir/basename $OUTFILE | grep "Max virtual memory" | awk -F' ' '{ print $4}'`
        NCPU=`cat $searchdir/basename $OUTFILE | grep "Number of cpus" | awk -F' ' '{ print $4}'`
        JOBFS=`cat $searchdir/basename $OUTFILE | grep "Max jobfs disk use" | awk -F' ' '{ print $5}'`
        WALLTIME=`cat $searchdir/basename $OUTFILE | grep "Elapsed time" | awk -F' ' '{ print $5}'`

     elif [ "$PACKAGE" == "gulp" ]
     then
        PRONAME=`cat $searchdir/basename $OUTFILE | grep "Project:" | awk -F: '{ print $2}' | awk '{for(i=1;i<=(NF-2);i++)print $i}'`
        VRAM=`cat $searchdir/basename $OUTFILE | grep "Max virtual memory" | awk -F' ' '{ print $4}'`
        NCPU=`cat $searchdir/basename $OUTFILE | grep "Number of cpus" | awk -F' ' '{ print $4}'`
        JOBFS=`cat $searchdir/basename $OUTFILE | grep "Max jobfs disk use" | awk -F' ' '{ print $5}'`
        WALLTIME=`cat $searchdir/basename $OUTFILE | grep "Elapsed time" | awk -F' ' '{ print $5}'`
     else
        PRONAME="Unknown Package enter Project Name"
     fi
  fi


  HEIGHT=$(cat $TMPDIR/file.$PACKAGE.send | wc -l)
  let HEIGHT=$HEIGHT*10

  if [ $HEIGHT -le 21 ]
  then
     HEIGHT=50
  fi

  if [ $HEIGHT -gt 70 ]
  then
     dialog --backtitle "DATASET SUBMISSION SCRIPT" --title "Do you want to update the Project Name " --inputbox "Project Name" 20 40 \
"$PRONAME" 2> $TMPDIR/metadata.$PACKAGE
  else
     dialog --backtitle "DATASET SUBMISSION SCRIPT" --title "Do you want to update the Project Name " --inputbox "Files: `cat $TMPDIR/file.$PACKAGE.send`" \
30 50 "$PRONAME" 2> $TMPDIR/metadata.$PACKAGE
  fi

  if [ $? -eq 0 ]
  then
     PRO=`cat $TMPDIR/metadata.$PACKAGE`
     if [[ -z $PRO ]]
     then
        dialog --msgbox "\nProject name should be entered.\nPress any key to proceed..." 15 15
        file_project
     else
        exec_summary
     fi
  else
     dialog --msgbox "\nError in displaying the project dialog box. The error code is $?.Press any key to go back to Main menu" \
15 40
     return
  fi
}

exec_summary(){

#Create exective summary file before submission

#Retrieve hostname and from hostname get the facility from different facilities
#TODO the below should be improved once more information is available 

  HOST=`hostname`

  if [ `echo $HOST | grep 'xe\|vayu'` ]
  then
     FACILITY="NCI"
  elif [ `echo $HOST | grep "vpac"` ]
  then
     FACILITY="VPAC"
  elif [ `echo $HOST | grep "its\|hpc"` ]
  then
     FACILITY="RMIT"
  else
     FACILITY="LOCALHOST"
  fi

#Retrieve username from the facility

  USERNAME=`whoami`
  FULLNAME=`finger $USERNAME | grep "Name" | awk '{for(i=4;i<=NF;i++)print $i}' | tr -s '\n' ' '`


#TODO to retrive the emailID from facility-need more info 

  EMAILID="$username@domain.edu.au"
  rm -f $TMPDIR/metadata.$PACKAGE

# The canned text consists of variables form output that has to be included.
# TODO - Make sure the varibales are not empty.


  DESC="This was a $PACKAGE calculation performed on $FACILITY, using $NCPU cpus, \
$VRAM of RAM, and $JOBFS of disk space for $WALLTIME hours, on $DATE.  The job consisted of $EXP, \
with $STOICHIOMETRY stoichiometry, and $NATOMS atoms, calculated using the $XCFUNCTIONAL functional with $CUTOFFTYPE \
set to $CUTOFF eV.  The run was performed by $FULLNAME under project $PRO."



  dialog --clear --backtitle "DATASET SUBMISSION SCRIPT" --title "Summary of your Submission" \
--form "Use [UP/Down] keys for editing" 20 70 10 "Username   :" 1 1 "$USERNAME" 1 14 20 30 "Name       :" 2 1 "$FULLNAME" 2 14 30 40 \
"Project    :" 4 1 "$PRO" 4 14 40 80 "Experiment :" 6 1 "$EXP" 6 14 40 80 "Description:" 8 1 "$DESC" 8 14 500 500 "Facility   :" 10 1 \
"$FACILITY" 10 14 30 30 2> $TMPDIR/metadata.$PACKAGE.tmp


  if [ $? -ne 0 ]
  then
     dialog --msgbox "\nNo files are submited for export. Press any key to continue..." 15 15
     return
  fi

# DESC=`echo $DESC | tr -s ':' '.'`

  TAG="Username~ Name~ Project~ Experiment~ Description~ Facility~"
  metadata=`cat $TMPDIR/metadata.$PACKAGE.tmp | tr -s '\n' '@' | tr -s '~' '-'`
  count=0
  for tag in $TAG
  do
    count=`expr $count + 1`
    echo "$tag `echo $metadata | awk -F@ '{ print $'$count' }'`"  >> $TMPDIR/metadata.$PACKAGE
  done

  rm -f $TMPDIR/metadata.$PACKAGE.tmp


#Check for directory counter file - if not create and increment the counter by 1.
#The spaces in the file name are replaced with underscores in order to name the file.

  FEXP=`echo $EXP | tr -s ' ' '_' | cut -c 1-10`

  if ! [[ -d $COUNTDIR ]]
  then
     mkdir $COUNTDIR
     filecount=1
     echo $filecount > $COUNTDIR/file.counter
     mkdir $EXPORTDIR/$FACILITY.$PACKAGE.$FEXP.$filecount
  else
     filecount=`cat $COUNTDIR/file.counter`
     echo `expr $filecount + 1` > $COUNTDIR/file.counter
     filecount=`cat $COUNTDIR/file.counter`
     mkdir $EXPORTDIR/$FACILITY.$PACKAGE.$FEXP.$filecount
  fi

  for item in `cat $TMPDIR/file.$PACKAGE.send`
  do
#    cp $searchdir/$item $EXPORTDIR/$FACILITY.$PACKAGE.$FEXP.$filecount
     ln -s $searchdir/$item $EXPORTDIR/$FACILITY.$PACKAGE.$FEXP.$filecount/$item
#    echo "$searchdir/$item $EXPORTDIR/$FACILITY.$PACKAGE.$filecount"
  done

# Included the 10 characters of expriment name and the counter in metadata file

  echo "FolderName~ $FEXP" >> $TMPDIR/metadata.$PACKAGE
  echo "Counter~ $filecount" >> $TMPDIR/metadata.$PACKAGE
  echo "Package~ $PACKAGE" >> $TMPDIR/metadata.$PACKAGE

  mv $TMPDIR/metadata.$PACKAGE $EXPORTDIR/$FACILITY.$PACKAGE.$FEXP.$filecount

  nohup `/bin/bash fta.sh` &>/dev/null &

  dialog --msgbox "\nFiles are placed in\nstaging area for automatic export. Check ~/Scripts/log for logfiles.Press any key to continue..." \
15 40

# TODO try including the files "Files : `cat file.$PACKAGE.send`" 6 1 "" 0 0 60 0

}

create_dir(){

  echo $1
  if ! [[ -d $1 ]]
  then
     echo "Creating required directories..."
     echo $1
     mkdir $1
  fi
}

create_config(){

  if ! [[ -e $BASEDIR/.config ]]
  then
    dialog --clear --backtitle "DATASET SUBMISSION SCRIPT" --title "Enter Tardis Credentials" \
--form "Use [UP/Down] keys for editing" 10 40 3 "Tardis Username :" 1 1 "$TUSERNAME" 1 20 20 20 "Tardis Password :" 2 1 \
"$TPASSWD" 2 20 20 20 2> $BASEDIR/.config
  fi


  TUSERNAME=`cat $BASEDIR/.config | head -1`
  TPASSWD=`cat $BASEDIR/.config | tail -1`

  if [[ $TUSERNAME == " " ]]
  then
     dialog --msgbox "\nUsername or Password cannot be empty. Press any key to continue..." 20 20
     rm -f $BASEDIR/.config
     create_config
  else
     TUSERNAME=`echo $TUSERNAME | tr -d '\n'`
     TPASSWD=`echo $TPASSWD | tr -d '\n'`
     login=`curl -F "username=$TUSERNAME" -F "password=$TPASSWD" -F "authMethod=localdb" $URL/apps/hpctardis/protocol/ 2> /dev/null`
  fi

  if [[ $login == "Successful" ]]
  then
     dialog --msgbox "\nYour tardis credentials are validated. Press any key to continue.." 20 20
     return
  else
     dialog --msgbox "\n$TUSERNAME or $TPASSWD is $incorrect. Please try again...." 20 20
     rm -f $BASEDIR/.config
     create_config
  fi

}


##############################################################################
#                           MAIN STRATS HERE                                 #
##############################################################################


trap 'error_function' ERR
trap 'deletetempfiles' EXIT     # calls deletetempfiles function on exit

# delete any residual tmp files before you start
deletetempfiles

#Check for base directory for the script to run, if not create it
create_dir "$BASEDIR"

#Check for tmp directory inside base directory to hold tmp files, if not create it
create_dir "$TMPDIR"

#Check for export directory inside base directory as staging area for the files, if not create it
create_dir "$EXPORTDIR"

#Check for log directory to hold the logfiles from file transfer agent
create_dir "$LOGDIR"

#10Sep2011
#Create Config file for Tardis as well for other protocols

create_config


clear

while :
do

# Menu to display theDialog utility to display options list
    deletetempfiles

    dialog --clear --backtitle "DATASET SUBMISSION SCRIPT" --title "SELECT PACKAGE" \
--menu "Use [UP/DOWN] key to move" 12 60 6 \
    "1"      "CRYSTAL" \
    "2"      "GULP" \
    "3"      "SIESTA" \
    "4"      "VASP" \
    "5"      "EXIT" 2> $TMPDIR/menuchoices.$$

    retopt=$?
    choice=`cat $TMPDIR/menuchoices.$$`

    case $retopt in

           0) case $choice in

                  1)  pack_files "crystal" ;;
                  2)  pack_files "gulp" ;;
                  3)  pack_files "siesta" ;;
                  4)  pack_files "vasp" ;;
                  5)  clear; exit 0 ;;

              esac ;;

          *)clear ; exit ;;
    esac

done
